<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/7/4
 * Time: 16:10
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Shou extends Base
{
    /**
     * 充值订单列表
     */
    public function index(){
        if(request()->isAjax ()){
            $mchid = input('mchid'); // 子商户号
            $order_number = input('order_number');
            $other_order_number = input('other_order_number');
            $user_username = input('user_username');
            $user_phone = input('user_phone');
            $project_id = input('project_id');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $dui = input('dui');

            $where['shou_is_pay'] = 2;

            if(isset($project_id) && $project_id != '') $where['shou_project_id'] = $project_id;
            if(isset($mchid) && $mchid != '') $where['mch_code'] = ['like', '%' . $mchid . '%'];
            if(isset($order_number) && $order_number != '') $where['shou_number'] = ['like', '%' . $order_number . '%'];
            if(isset($other_order_number) && $other_order_number != '') $where['shou_other_number'] = ['like', '%' . $other_order_number . '%'];
            if(isset($user_username) && $user_username != '') $where['u1.user_real_name'] = ['like', '%' . $user_username . '%'];
            if(isset($user_phone) && $user_phone != '') $where['u1.user_phone'] = ['like', '%' . $user_phone . '%'];
            if(isset($dui) && $dui != '') $where['shou_dui'] = $dui;
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['shou_pay_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['shou_pay_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['shou_pay_time'] = ['<=', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_shou')
                ->join('up_u_user u1', 'shou_user_id = u1.user_id', 'LEFT')
                ->join('up_mchid', 'shou_mchid = mch_id', 'LEFT')
//                ->join('up_u_user u2', 'u2.user_id = shou_user', 'LEFT')
                ->where($where)
                ->count('shou_id');
            $lists = Db::table('up_shou')
                ->join('up_u_user u1', 'shou_user_id = u1.user_id', 'LEFT')
                ->join('up_mchid', 'shou_mchid = mch_id', 'LEFT')
//                ->join('up_u_user u2', 'u2.user_id = shou_user', 'LEFT')
                ->field([
                    'shou_id',
                    'u1.user_real_name',
                    'u1.user_phone',
                    'mch_code',
                    'shou_number',
                    'shou_other_number',
                    'shou_price',
                    'shou_project_name',
                    'shou_remark',
                    'shou_pay_time',
                    'shou_dui',
//                    'u2.user_real_name do_user',
                    'shou_admin_id'
                ])
                ->where($where)
                ->order('shou_pay_time desc')
                ->page($Nowpage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $p = Db::table('up_bo_in')->where('status', 1)->field('id, name')->select();
        return $this->fetch('', ['p' => $p]);
    }

    /**
     * 对账
     * @return \think\response\Json
     */
    public function dui(){
        $id = input('id');
        $res = Db::table('up_shou')->where('shou_id', $id)->update(['shou_dui' => 2, 'shou_admin_id' => session('username')]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 编辑备注
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateRemark(){
        $shou_id = input('shou_id');
        if(Request::instance()->isPost()){
            $remark = input('remark');

            $update = [
                'shou_id' => $shou_id,
                'shou_remark' => $remark
            ];
            $res = Db::table('up_shou')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $data = Db::table('up_shou')->where('shou_id', $shou_id)->field('shou_remark')->find();
        return $this->fetch('', ['shou_id' => $shou_id, 'data' => $data]);
    }

    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $order_ids = input('ids');

        $where['shou_is_pay'] = 2;

        if($order_ids == -1){
            $mchid = input('mchid'); // 子商户号
            $order_number = input('order_number');
            $other_order_number = input('other_order_number');
            $user_username = input('user_username');
            $user_phone = input('user_phone');
            $project_id = input('project_id');
            $start_time = input('start_time');
            $end_time = input('end_time');
            $dui = input('dui');

            if(isset($project_id) && $project_id != '') $where['shou_project_id'] = $project_id;
            if(isset($mchid) && $mchid != '') $where['mch_code'] = ['like', '%' . $mchid . '%'];
            if(isset($order_number) && $order_number != '') $where['shou_number'] = ['like', '%' . $order_number . '%'];
            if(isset($other_order_number) && $other_order_number != '') $where['shou_other_number'] = ['like', '%' . $other_order_number . '%'];
            if(isset($user_username) && $user_username != '') $where['u1.user_real_name'] = ['like', '%' . $user_username . '%'];
            if(isset($user_phone) && $user_phone != '') $where['u1.user_phone'] = ['like', '%' . $user_phone . '%'];
            if(isset($dui) && $dui != '') $where['shou_dui'] = $dui;
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['shou_pay_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['shou_pay_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['shou_pay_time'] = ['<=', $end_time];
            }
        }else{
            if($order_ids == ''){
                return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            }
            $where['shou_id'] = ['IN', $order_ids];
        }

        $lists = Db::table('up_shou')
            ->join('up_u_user u1', 'shou_user_id = u1.user_id', 'LEFT')
            ->join('up_mchid', 'shou_mchid = mch_id', 'LEFT')
//            ->join('up_u_user u2', 'u2.user_id = shou_user', 'LEFT')
            ->field([
                'shou_id',
                'u1.user_real_name',
                'u1.user_phone',
                'mch_code',
                'shou_number',
                'shou_other_number',
                'shou_price',
                'shou_project_name',
                'shou_remark',
                'shou_pay_time',
//                'u2.user_real_name do_user',
                'shou_admin_id'
            ])
            ->where($where)
            ->order('shou_pay_time desc')
            ->select();

        $cellname = [
            '收款ID',
            '下单用户',
            '下单用户手机号',
            '子商户号',
            '订单号',
            '第三方订单号',
            '收款金额',
            '收款类型',
            '备注',
            '支付时间',
//            '前端对账',
            '后端对账'
        ];
        $res = exportExcel('Shou', time(), $cellname, $lists);
        return json($res);
    }
}